<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/21
 * Time: 14:03
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Member extends Api
{

    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    //新增用户
    public function userAdd(){
        $nickname = input('post.nickname');
        $openID   = input('post.openID');
        $image    = input('post.image');
        $pid      = input('post.pid')? input('post.pid'): 0;
        $data = [
            'nickname'=>$nickname,
            'image'=>$image,
            'openid'=>$openID,
            'pid'   =>$pid,
            'time'  =>time(),
            'addr_id'=>0,
            'status'=>0
        ];
        $ret  = Db::name('member_user')->insertGetId($data);
        if ($ret){
            $this->error('请求失败');
        }
        $this->success('请求成功',$ret);
    }

    //个人中心
    public function userInfo(){
        $user_id = input('post.user_id');
        $user = Db::name('member_user')->where('id',$user_id)->find();
        if (!empty($user)){
            $this->error('无数据');
        }
        $this->success('请求成功',$user);
    }
    //订单状态
    public function orderInfo(){
        $user_id = input('post.user_id');
        $data['wzf'] = Db::name('order_sku')->where('user_id',$user_id)->where('status',0)->count();
        $data['dfh'] = Db::name('order_sku')->where('user_id',$user_id)->where('status',0)->count();
        $data['dsh'] = Db::name('order_sku')->where('user_id',$user_id)->where('status',0)->count();
        $data['ywc'] = Db::name('order_sku')->where('user_id',$user_id)->where('status',0)->count();
        $data['sh'] = Db::name('order_sku')->where('user_id',$user_id)->where('status',0)->count();
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //我的优惠劵
    public function coupon(){
        $user_id = input('post.user_id');
        $status  = input('post.status');
        $data = Db::name('member_discount')->where('user_id',$user_id)->where('status',0)->select();
        if ($status > 0){
            $data = Db::name('member_discount')->where('user_id',$user_id)->where('status','>',0)->select();
        }
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //地址列表
    public function myAddress(){
        $user_id = input('post.user_id');
        $data = Db::name('address')->where('user_id',$user_id)->select();
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //新增地址
    public function addressAdd(){
        $user_id   = input('post.user_id');
        $addr_name = input('post.addr_name');
        $phone     = input('post.phone');
        $address   = input('post.address');
        $info      = input('post.info');
        if (!$user_id && !$addr_name && !$phone && !$address && !$info){
            $this->error('参数不能为空');
        }
        $data = [
            'user_id'=>$addr_name,
            'addr_name'=>$addr_name,
            'phone'  =>$phone,
            'address'=>$address,
            'info'   =>$info,
            'status' => 0
        ];
        $insert = Db::name('address')->insert($data);
        if (!$insert){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //修改地址
    public function addressEdit(){
        $user_id  = input('post.user_id');
        $id  = input('post.addr_id');
        $data     = input('post.data');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        $ret = Db::name('address')->where(['id'=>$id,'user_id'=>$user_id])->update($data);
        if (!$ret){
            $this->error('修改失败');
        }
        $this->success('请求成功');
    }

    //设为默认
    public function addressTrue(){
        $user_id  = input('post.user_id');
        $id  = input('post.addr_id');
        $updata = Db::name('address')->where(['user_id'=>$user_id,'status'=>1])->find();
        if ($updata){
             Db::name('address')->where(['id'=>$updata['id'],'user_id'=>$user_id])->update(['status'=>0]);
        }
        $ret = Db::name('address')->where(['id'=>$id,'user_id'=>$user_id])->update(['status'=>1]);
        $member = Db::name('member_user')->where(['user_id'=>$user_id])->update(['addr_id'=>$id]);
        if (!$ret && !$member){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //删除地址
    public function addressDel(){
        $user_id  = input('post.user_id');
        $id  = input('post.addr_id');
        $ret = Db::name('address')->where(['id'=>$id,'user_id'=>$user_id])->find();
        if ($ret['status'] == 1){
            Db::name('member_user')->where(['user_id'=>$user_id])->update(['addr_id'=>0]);
        }
        $del = Db::name('address')->where(['id'=>$id,'user_id'=>$user_id])->delete();
        if (!$del){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }






}